<?=view('header.html')?>

<body class="layui-view-body">
  <div class="layui-content">
    <div class="layui-row">
      <div class="layui-card">

        <div class="layui-card-header">
          会员分组
        </div>

        <div class="layui-card-body">
          <table id="listTable" lay-filter="listTable"></table>

          <script type="text/html" id="toolbarOption">
            <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="delAll">删除</a>
            <a class="layui-btn layui-btn-sm" lay-event="add">添加会员组</a>
          </script>

          <script type="text/html" id="barOption">
            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
          </script>

        </div>
      </div>
    </div>
  </div>
  <?=view('footer.html')?>
  <script type="text/javascript">

    layui.use(['layer', 'form', 'jquery', 'table'], function () {
      var layer = layui.layer,
        $ = layui.jquery,
        form = layui.form,
        table = layui.table;
      


      //列表
      var tableInit = table.render({
        elem: '#listTable',
        toolbar: "#toolbarOption",
        defaultToolbar: [],
        title: '支付列表',
        url: adminpath + "admin/user/group",
        method: 'post',
        cols: [
          [
            { type: 'checkbox' },
            { field: 'id', title: 'ID', width: 80, sort: true },
            { field: 'name', title: '分组名称' },
            { field: 'price', title: '价格' },
            { field: 'limit', title: '价格单位' },
            { field: 'overdue', title: '过期后' },
            { field: 'allowregister', title: '允许注册' },
            { fixed: 'right', title: '操作', toolbar: '#barOption' }
          ]
        ],
        page: {
          layout: ['prev', 'page', 'next', 'count'],
          groups: 5,
          first: true,
          last: true
        },
        response: {
          statusCode: 0,
          countName: 'count'
        },
        parseData: function (res) {
          return {
            'code': res.code,
            "data": res.data.data,
            'count': res.data.total
          };
        }
      });


      //查询提交
      form.on('submit(mokSearch)', function (data) {
        tableInit.reload({
          where: data.field
          , page: {
            curr: 1
          }
        });

      });

      //重置search_reset
      form.on('submit(mokSearchReset)', function (data) {
        $('#search')[0].reset();
        form.render(null, 'formSearch');
        var field = data.field;
        $.each(field, function (k, v) {
          field[k] = '';
        });
        tableInit.reload({
          where: data.field
          , page: {
            curr: 1
          }
        });
      });

      //头工具栏事件
      table.on('toolbar(listTable)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
          case 'delAll':
            var data = checkStatus.data;
            var ids = [];
            data.forEach(function (n, i) {
              ids.push(n.uid);
            });

            if (ids != '') {

              layer.confirm('确定要删除吗？', { icon: 3, title: '消息提示' }, function (index) {
                $.post(adminpath + "admin/user/delGroup", { ids: ids }, function (res) {
                  if (res.code == 1) {
                    layer.msg('操作成功， 正在刷新...', {
                      time: 1500, end: function () {
                        tableInit.reload('listTable');
                      }
                    });
                  } else {
                    layer.msg(res.msg);
                  }
                }, 'json')
                layer.close(index);
              });

            } else {
              layer.msg('请选择要操作的数据', { icon: 5, anim: 6 });
              return false;
            }
            break;

          case 'add':
            var url = adminpath + 'admin/user/addGroup';
            var title = '添加用户分组';
            layer.open({
              title: title,
              type: 2,
              area: ['600px', '80%'],
              anim: 2,
              shadeClose: false,
              skin: 'layui-layer-molv',
              content: url
            });
            break;

        };
      });

      //监听行工具事件
      table.on('tool(listTable)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
          layer.confirm('确定要删除吗？', { icon: 3, title: '消息提示' }, function (index) {
            $.post(adminpath + "admin/user/delGroup", { ids: data.id }, function (res) {
              if (res.code == 1) {
                layer.msg('操作成功， 正在刷新...', {
                  time: 1500, end: function () {
                    tableInit.reload();
                  }
                });
              } else {
                layer.msg(res.msg);
              }
            }, 'json')
            layer.close(index);
          });

        } else if (obj.event === 'edit') {
          var url = adminpath + 'admin/user/editGroup?id=' + data.id;
          var title = '修改用户分组';
          layer.open({
            title: title,
            type: 2,
            area: ['600px', '80%'],
            anim: 2,
            shadeClose: false,
            skin: 'layui-layer-molv',
            content: url
          });

        }
      });

      //搜索条件
      form.on('submit(ams_reset)', function (data) {
        $("#searchForm")[0].reset();
        form.render();
        $('#ams_search').trigger('click');
      });

      //标题搜索
      form.on('submit(ams_search)', function (data) {
        tableInit.reload({
          where: data.field
          , page: {
            curr: 1
          }
        });
      });



    });

  </script>

</body>

</html>
